
import com.etop.weibo.beans.Comment;
import com.etop.weibo.dao.CommentDao;
import com.etop.weibo.dao.DAOFactory;
import com.etop.weibo.dto.UserCommentDTO;
import com.etop.weibo.util.HibernateUtil;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.hibernate.Session;
import org.junit.Test;

/**
 *
 * @author jansing
 */
public class CommentCRUD {
    
    public void deleteAll03(){
//        new CommentDao().testDeleteAll(3);
    }
    
    public void deleteAll02(){
        DAOFactory.getCommentDao().deleteAll(2);
    }
    public void deleteAll01(){
        DAOFactory.getCommentDao().deleteAll(1);
    }
    
    public void delete01(){
        DAOFactory.getCommentDao().delete(233);
    }
    
    public void praise(){
        Session session = null;
        
        session = HibernateUtil.openSession();
        session.beginTransaction();
        session.createQuery("update Comment set praise=praise+1 where id=2").executeUpdate();
        session.getTransaction().commit();
        session.close();
    }
    
    public void list01(){
        List<UserCommentDTO> list = DAOFactory.getCommentDao().list(2);
        for(UserCommentDTO comm : list){
            System.out.println(comm.getUserId() + " -> " + comm.getNickname()+ " : " + comm.getWhat() + comm.getPraise());
        }
    }
    
    @Test
    public void load01(){
        Comment comm = DAOFactory.getCommentDao().load(5);
            System.out.println(comm.getCreateTime().getTime()+ "  " 
                    + comm.getUserId() + " -> " + comm.getSayId() + " : " + comm.getWhat());
    }
    
    public void add02(){
        for(int i=1; i<10;i++){
            Comment comm = new Comment();
            comm.setCreateTime(new Date());
            Comment comm1 = DAOFactory.getCommentDao().load(1);
            comm.setListTime(comm1.getListTime());
            comm.setSayId(comm1.getSayId());
            comm.setCommentId(comm1.getId());
            comm.setUserId(1);
            comm.setPraise(33);
            comm.setWhat("every day");
            DAOFactory.getCommentDao().add(comm);
        }
    }
    
    public void add01() {
        for (int i = 1; i < 20; i++) {
            Comment comm = new Comment();
            comm.setCreateTime(new Date());
            comm.setListTime(new Date());
            comm.setSayId(i);
            comm.setUserId(1);
            comm.setWhat("rock and roll");
            DAOFactory.getCommentDao().add(comm);
        }
    }
}
